import Vue from "vue";
import App from "./App";
import router from "./router";
import ViewUI from "view-design";
import "./assets/styles/theme.less";
// import './assets/iconfont/iconfont.css';
import * as filters from "./plugins/filters";
import store from "@/vuex/store";
import storage from "@/plugins/storage";
// 全局引入封装组件
import { InstallAll } from "@/components/global.js";
import Cookies from 'js-cookie';
import { md5 } from '@/plugins/md5.js';
import { create as createTIM } from '@/plugins/tim/tim-js';
import TIMUploadPlugin from '@/plugins/tim/tim-upload-plugin';

Vue.prototype.md5 = md5;


/**
 * 
 * 腾讯云 即时通讯IM
 * 
 * */

let options = {
  SDKAppID: 1400741815 // 接入时需要将0替换为您的即时通信 IM 应用的 SDKAppID
};
// 创建 SDK 实例，`TIM.create()`方法对于同一个 `SDKAppID` 只会返回同一份实例
let tim = createTIM(options); // SDK 实例通常用 tim 表示
// 设置 SDK 日志输出级别，详细分级请参见 setLogLevel  https://web.sdk.qcloud.com/im/doc/zh-cn/SDK.html#setLogLevel 接口的说明</a>
tim.setLogLevel(1); // 普通级别，日志量较多，接入时建议使用
// 注册腾讯云即时通信 IM 上传插件
tim.registerPlugin({ 'tim-upload-plugin': TIMUploadPlugin });
Vue.prototype.$tim = tim;


const { aMapSecurityJsCode, title } = require("@/config");
Vue.use(ViewUI);
Vue.use(InstallAll);
Vue.config.productionTip = false;

Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key]);
});

// 高德安全密钥
if (aMapSecurityJsCode) {
  window._AMapSecurityConfig = {
    securityJsCode: aMapSecurityJsCode,
  };
}

router.beforeEach((to, from, next) => {
  // const name = to.name;
  // if (!Cookies.get('userInfoManager') && name !== 'login') {
  //   // 判断是否已经登录且前往的页面不是登录页
  //   next({
  //     name: 'login'
  //   });
  // } else if (Cookies.get('userInfoManager') && name === 'login') {
  //   // 判断是否已经登录且前往的是登录页
  //   next({
  //     name: 'home_index'
  //   });
  // }
  // next();
  ViewUI.LoadingBar.start();
  // window.document.title = to.meta.title === undefined ? title : to.meta.title;
  next();
});

router.afterEach((route) => {
  ViewUI.LoadingBar.finish();
});

Vue.prototype.linkTo = function (url) {
  
  if (url.substr(0, 1) === "/") {
    router.push(url)
    // 非外部链接，没有origin，只有路由地址
    // if (router.mode === "hash") {
    //   window.open(location.origin + "/#" + url, "_blank");
    // } else {
    //   window.open(location.origin + url, "_blank");
    // }
  } else {
    // 外部链接，完整的url地址
    window.open(url, "_blank");
  }
};

Vue.prototype.$addStorageEvent = (key, data)=> {
  // 创建一个StorageEvent事件
  var newStorageEvent = document.createEvent('StorageEvent')
  const storage = {
    setItem: function(k, val) {
      localStorage.setItem(k, val)
      // 初始化创建的事件
      newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null)
      // 派发对象
      window.dispatchEvent(newStorageEvent)
    }
  }
  return storage.setItem(key, data)
}

// 日期格式处理
Date.prototype.format = function(fmt) { 
  var o = { 
     "M+" : this.getMonth()+1,                 //月份 
     "d+" : this.getDate(),                    //日 
     "H+" : this.getHours(),                   //小时 
     "m+" : this.getMinutes(),                 //分 
     "s+" : this.getSeconds(),                 //秒 
     "q+" : Math.floor((this.getMonth()+3)/3), //季度 
     "S"  : this.getMilliseconds()             //毫秒 
 }; 
 if(/(y+)/.test(fmt)) {
         fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
 }
  for(var k in o) {
     if(new RegExp("("+ k +")").test(fmt)){
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
      }
  }
 return fmt; 
} 

// 联系客服
Vue.prototype.connectCs = function (
  sign = "37ef9b97807d03c6741298ed4eb5b536d2d238e08a3c00fb01fe48f03a569974c99ad767e72c04b3165ef29aca2c488b505fe4ca"
) {
  const url = "https://yzf.qq.com/xv/web/static/chat/index.html?sign=" + sign;
  window.open(url, "_blank");
};
Vue.prototype.Cookies = storage;
/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
